<?php
###############   COPYLEFT GPLv3 LICENSE   ###############
##
## JFX Version 0.2.9
## Website Management Software
## www.jfxcms.com
##
## Copyright 2009 GPLv3 - http://www.opensource.org/licenses/gpl-3.0.html
##
## Anthony Gallon
## oi_antz@hotmail.com
##
## Permission is hereby granted to any person having a copy of this software
## to freely use and modify as required so long as the copyright notices
## and branding remain intact.
##
## Full license details available at http://www.jfxcms.com/license
##
###############   COPYLEFT GPLv3 LICENSE   ###############



$tagname = preg_replace('/[^a-zA-Z0-9\_]/', '', get('tag'));

$THEME->addBreadcrumb($this->adminUrl.'/edit-tag/?tag='.$tagname, $this->lang('edit_tag'));
$THEME->addBreadcrumb($this->adminUrl.'/edit-tag/?tag='.$tagname, $tagname);


if($DB->countRows($CONFIG->dbprefix.'cst_tags', "tagname = '{$tagname}'")==0){
    JFX::addError($tagname.' '.$this->lang('selected_tag_doesnt_exist'));
    return;
}



$tData = $DB->fetchRow("SELECT * FROM {$CONFIG->dbprefix}cst_tags WHERE tagname = '{$tagname}'");




if(Antz_IntelliForm::submitted('edit-tag')){
    $error = false;
    $newTagname = preg_replace('/[^a-zA-Z0-9\_]/', '', post('tagname'));
    $isBlock = (int) post('isblock');

    if($isBlock > 1) $isBlock = 1;
    if($isBlock < 0) $isBlock = 0;
    
    if(trim($tagname)==''){
        $error = true;
        JFX::addFormError('tagname', $this->lang('cannot_be_empty'));
        JFX::addError($this->lang('cannot_be_empty'));
    }

    if($DB->countRows($CONFIG->dbprefix.'cst_tags', "tagname = '{$newTagname}'")>0 && $newTagname != $tagname){
        $error = true;
        JFX::addFormError('tagname', $this->lang('already_registered'));
        JFX::addError($this->lang('already_registered'));
    }

    if(!$error){
        $dbParams = array(
            'tagname' => $newTagname,
            'code' => $_POST['tagcontent'],
            'is_block' => $isBlock
        );
        $DB->update($CONFIG->dbprefix.'cst_tags', $dbParams, "tagname = '{$tagname}'");
        JFX::addSuccess($this->lang('updated_ok'));
        die('<script type="text/javascript">
if(window.parent) window.parent.location = "'.$url.'";
</script>');
    }






}





$formFields = array(
    array(
        'name' => 'tagname',
        'value' => post('tagname', $tData['tagname']),
        'type' => 'text',
        'label' => $this->lang('tag_name')
    ),
    array(
        'name' => 'isblock',
        'type' => 'checkbox',
        'label' => $this->lang('is_block'),
        'value' => 1,
        'checked' => (bool) $tData['is_block']
    ),
    array(
        'name' => 'tagcontent',
        'value' => array_key_exists('tagcontent', $_POST) ? $_POST['tagcontent'] : $tData['code'],
        'type' => 'code',
        'id' => 'tagcontent',
        'class' => 'php',
        'label' => $this->lang('form_code')
    ),
    array(
        'type' => 'submit',
        'value' => $this->lang('form_save'),
        'label' => '&nbsp;',
        'name' => 'submitBtn'
    )
);

echo JFX::makeRapidForm($formFields, $this->lang('form_legend_edit'), 'edit-tag');




